package com.example.server.db.tables

import com.example.commons.core.longValue
import com.example.server.db.tables.Roles.defaultExpression
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.javatime.datetime
import java.time.LocalDateTime

object Roles : Table() {

    val id = long("id").autoIncrement()

    val roleName = varchar("roleName", 64)

    /**
     * admin/user/...
     */
    val roleKey = varchar("roleKey", 128)
    val enabled = bool("isEnabled").default(true)
    val createdAt = datetime("createdAt").clientDefault {
        LocalDateTime.now()
    }

    val remark = text("remark").nullable()

    override val primaryKey = PrimaryKey(id)
}

object UserRole : Table() {
    val userId = long("userId").references(Users.id)
    val roleId = long("roleId").references(Roles.id)
    val createdAt = datetime("createdAt").clientDefault {
        LocalDateTime.now()
    }
//    val updatedAt = long("updatedAt").defaultExpression(
//        CustomFunction("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", LongColumnType())
//    )

    override val primaryKey: PrimaryKey
        get() = PrimaryKey(userId, roleId)
}